package com.lzx.iteam.net;

import android.content.Context;
import android.os.Message;
import android.util.Log;
import com.lzx.iteam.bean.ApprovalData;
import com.lzx.iteam.provider.CloudDBOperation;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.DateUtil;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuepengju on 14-3-20.
 */
public class GetApprovalMsg extends CcMsgStructure {

    private Message mCallback;
    private int mListType;
    private String mUid;
    private CloudDBOperation mCloudDBOperation;
    private Context mContext;
    private String mGroupId;

    public GetApprovalMsg(Message callback,String groupId,int listType,String mUid,Context context){
        mCallback = callback;
        this.mGroupId = groupId;
        this.mListType = listType;
        this.mUid = mUid;
        this.mContext = context;
        mCloudDBOperation = new CloudDBOperation(mContext);
    }

    @Override
    public void onReceive(JSONObject data) {

        List<ApprovalData> mListData = new ArrayList<ApprovalData>();
        try {
            JSONObject jsonObject = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            JSONArray jsonArray=jsonObject.getJSONArray("attendances");
            mCloudDBOperation.deleteAllApproval(mUid,mListType,mGroupId);
            for (int i = 0;i<jsonArray.length();i++){
                ApprovalData approvalData = new ApprovalData();
                JSONObject approval = jsonArray.getJSONObject(i);
                approvalData.setUser_id(approval.getString("user_id"));
                approvalData.setUser_name(approval.getString("user_name"));
                approvalData.setUser_image(approval.getString("user_image"));
                approvalData.setApproval_id(approval.getString("_id"));
                approvalData.setApproval_group_id(approval.getString("approval_group_id"));
                if (approval.has("approval_type")){
                    approvalData.setApproval_type(approval.getInt("approval_type"));
                }
                if (approval.has("approval_leave_type")){
                    approvalData.setApproval_leave_type(approval.getInt("approval_leave_type"));
                }
                if (approval.has("approval_address")){
                    approvalData.setApproval_address(approval.getString("approval_address"));
                }
                if (approval.has("approval_start_time")){
                    approvalData.setApproval_start_time(approval.getString("approval_start_time"));
                }
                if (approval.has("approval_end_time")){
                    approvalData.setApproval_end_time(approval.getString("approval_end_time"));
                }
                if (approval.has("approval_start_part")){
                    approvalData.setApproval_start_part(approval.getInt("approval_start_part"));
                }
                if (approval.has("approval_end_part")){
                    approvalData.setApproval_end_part(approval.getInt("approval_end_part"));
                }
                if (approval.has("approval_day")){
                    approvalData.setApproval_day(Float.parseFloat(approval.getString("approval_day")));
                }
                if (approval.has("approval_reason")){
                    approvalData.setApproval_reason(approval.getString("approval_reason"));
                }
                if (approval.has("approval_dates")){
                    approvalData.setApproval_dates(approval.getString("approval_dates"));
                }
                approvalData.setApprove_admin(approval.getString("approve_admin"));
                approvalData.setApprove_admin_name(approval.getString("approve_admin_name"));
                approvalData.setApprove_admin_image(approval.getString("approve_admin_image"));
                if (approval.has("approval_done_time")){
                    approvalData.setApproval_done_time(approval.getString("approval_done_time"));
                }
                String createTime = approval.getString("approval_time");
                approvalData.setApproval_time(createTime);
                approvalData.setYear(DateUtil.getYear(Long.parseLong(createTime) * 1000));
                approvalData.setMonth(DateUtil.getMonth(Long.parseLong(createTime) * 1000));

                approvalData.setApproval_status(approval.getInt("approval_status"));
                if (approval.has("refuse_reason")){
                    approvalData.setApproval_refuse_reason(approval.getString("refuse_reason"));
                }
                if (approval.has("approval_cancel")){
                    approvalData.setApproval_cancel(approval.getInt("approval_cancel"));
                }else{
                    approvalData.setApproval_cancel(3);
                }
                if (approval.has("approval_cancel_reason")) {
                    approvalData.setApproval_cancel_reason(approval.getString("approval_cancel_reason"));
                }
                if (approval.has("approval_cancel_time")){
                    approvalData.setApproval_cancel_time(approval.getString("approval_cancel_time"));
                }
                if (approval.has("approval_cancel_done_time")){
                    approvalData.setApproval_cancel_done_time(approval.getString("approval_cancel_done_time"));
                }

                mListData.add(approvalData);
                mCloudDBOperation.insertApprovalListData(approvalData,mListType,mUid);

            }
            if(mCallback != null){
                mCallback.arg1 = 0;
                mCallback.obj = mListData;
                mCallback.sendToTarget();
            }
        }catch (Exception e){
            Log.e("All_Exception","GetApprovalMsg == " + e.toString());
        }


    }

    @Override
    public void onError(int error, Object ret) {
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if(mCallback != null){
            mCallback.arg1 = error;
            if(!StringUtil.isEmpty(errorMsg)){
                mCallback.obj = errorMsg;
            }else{
                mCallback.obj = Constants.NETWORK_WRROR;
            }
            mCallback.sendToTarget();
        }
    }
}
